home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: States and Capitals module
- *
- * This module is an example of an extremely simple interrogative
- * scenario. The original thirteen colonies of the United States must
- * be matched with their capital cities. There is a single button that
- * says "Give Up?" If the user clicks the button, the answer is shown,
- * the button's title changes to "Resume", and the scenario is frozen
- * until the button is clicked. Then the answer is cleared, the capitals
- * are shuffled, and the user may start over.
- *
- * If the correct answers are all gotten, the button again changes to
- * "Resume" and the scenario is frozen until the button is clicked.
- *
- * The module is unexciting; it's meant as a basic illustration, rather
- * than anything useful. There are lots of bells and whistles that
- * could be added.
- *
- * 26 July 1986 Paul DuBois
- *
- * 29 Dec 93
- * - Convert blobs from picture blobs to procedure blobs.
- * This gives better dimming on monitor that can do grayscale
- * and turns out to be a lot faster.
- */
-
- # include "TransSkel.h"
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- # define nStates 13
-
- # define hState1 220
- # define hState2 110
- # define vState 16
- # define stateXOff 10
- # define stateYOff 5
- # define hCapital 85
- # define vCapital 16
- # define capXOff 225
- # define capYOff 5
-
-
- struct info
- {
- unsigned char *stateName;
- unsigned char *capName;
- };
-
-
- static WindowPtr wind;
- static FontInfo fontInfo;
- static BlobSetHandle donors; /* donor blobs */
- static BlobSetHandle receptors; /* receptor blobs */
- static ControlHandle giveUp;
- static Boolean paused = false;
-
- static struct info scPair[nStates] =
- {
- { "\pConnecticut", "\pHartford" },
- { "\pDelaware", "\pDover" },
- { "\pGeorgia", "\pAtlanta" },
- { "\pMaryland", "\pAnnapolis" },
- { "\pMassachusetts", "\pBoston" },
- { "\pNew Hampshire", "\pConcord" },
- { "\pNew Jersey", "\pTrenton" },
- { "\pNorth Carolina", "\pRaleigh" },
- { "\pNew York", "\pAlbany" },
- { "\pPennsylvania", "\pHarrisburg" },
- { "\pRhode Island", "\pProvidence" },
- { "\pSouth Carolina", "\pColumbia" },
- { "\pVirginia", "\pRichmond" }
- };
-
-
- static void
- DrawBlobString (Rect *r, StringPtr str)
- {
- short h, v;
-
- h = (r->left + r->right - StringWidth (str)) / 2;
- v = r->bottom - fontInfo.descent;
- MoveTo (h, v);
- DrawString (str);
- }
-
- /*
- * Donor blobs have empty static regions, so this is only called to draw
- * the drag region (the capital name).
- */
-
- static pascal void
- DrawDonor (BlobHandle bDst, BlobHandle bSrc, short partCode)
- {
- Rect r;
-
- r = BDragBox (bDst);
- EraseRect (&r);
- DrawBlobString (&r, scPair[GetBRefCon (bSrc)].capName);
- }
-
-
- static pascal void
- DrawReceptor (BlobHandle bDst, BlobHandle bSrc, short partCode)
- {
- Rect r;
-
- if (partCode == inDragBlob)
- {
- r = BDragBox (bDst);
- EraseRect (&r);
- FrameRect (&r);
- }
- else
- {
- r = BStatBox (bDst);
- EraseRect (&r);
- DrawBlobString (&r, scPair[GetBRefCon (bSrc)].stateName);
- }
- }
-
-
- static void
- MakeBlobs (void)
- {
- short i;
- Rect r, r2;
- BlobHandle b1, b2;
-
- donors = NewBlobSet ();
- receptors = NewBlobSet ();
- for (i = 0; i < nStates; ++i)
- {
- b1 = NewBlob (donors, true, 1, false, (long) i);
- b2 = NewBlob (receptors, true, 0, true, (long) i);
- NewBlobMatch (b1, b2); /* attach the answer */
-
- SetRect (&r, 0, 0, hCapital, vCapital);
- SetProcRectBlob (b1, DrawDonor, &r, &r);
- MoveBlob (b1, inFullBlob, capXOff, capYOff + (vCapital + 2) * i);
-
- SetRect (&r, 0, 0, hState2, vState);
- SetRect (&r2, 0, 0, hCapital, vCapital);
- OffsetRect (&r2, hState2 + 5, 0);
- SetProcRectBlob (b2, DrawReceptor, &r2, &r);
- MoveBlob (b2, inFullBlob, stateXOff, stateYOff + (vState + 2) * i);
- }
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- BlobHandle b, d;
- ControlHandle ctl;
-
- if (FindControl (pt, wind, &ctl))
- {
- if (TrackControl (ctl, pt, nil))
- {
- if (paused) /* either all answers are correct, or we're */
- { /* showing the answer. Either way, restore */
- ThawBlobSet (receptors); /* to start state */
- ThawBlobSet (donors);
- ZUnglueGlobSet (receptors);
- ShuffleBlobSet (receptors);
- ShuffleBlobSet (donors);
- SetCTitle (giveUp, "\pGive Up?");
- ValidRect (&wind->portRect);
- paused = false;
- }
- else /* user gives up - show answer */
- {
- for (b = FirstBlob (receptors); b != nil; b = NextBlob (b))
- ZGlueGlob (FirstBMatch (b), b);
- FreezeBlobSet (receptors);
- FreezeBlobSet (donors);
- SetCTitle (giveUp, "\pResume");
- paused = true;
- }
- }
- }
- else
- {
- BlobClick (pt, t, donors, receptors);
- if (!paused && BlobSetQuiet (receptors)) /* answers correct? */
- {
- FreezeBlobSet (receptors);
- FreezeBlobSet (donors);
- SetCTitle (giveUp, "\pResume");
- paused = true;
- }
- }
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- DrawControls (wind);
- DrawBlobSet (receptors);
- DrawBlobSet (donors);
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (true, true, false, true, true);
- }
- }
-
-
- void
- StatesInit (void)
- {
- Rect r;
-
- SkelWindow (wind = GetDemoWind (statesWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- GetFontInfo (&fontInfo);
-
- MakeBlobs ();
-
- r = wind->portRect;
- SetRect (&r, r.left + 5, r.bottom - 25, r.left + 75, r.bottom - 5);
- giveUp = NewControl (wind, &r, "\pGive Up?", true, 0, 0, 0,
- pushButProc, 0L);
-
- MakeFrontWind (wind); /* show window */
- SkelPause (30L); /* wait a bit */
- ShuffleBlobSet (donors); /* scramble answers */
- SkelDoUpdates (); /* process update resulting from scramble */
- }
-